package com.settlement.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.WmNoRecord;
import com.settlement.system.service.WmNoRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.settlement.system.model.query.WmNoRecordQuery;
import com.settlement.system.model.form.WmNoRecordForm;
import org.springdoc.core.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.Parameter;
import com.settlement.system.model.vo.WmNoRecordImportVo;
import com.settlement.system.listener.easyexcel.WmNoRecordListener;
import com.alibaba.excel.EasyExcel;

import com.alibaba.excel.EasyExcelFactory;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 微盟交易单号表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/api/v1/wmNoRecord")
@RequiredArgsConstructor
public class WmNoRecordController {

    private final WmNoRecordService wmNoRecordService;

    @GetMapping("/page")
    public PageResult<WmNoRecord> getPage(@ParameterObject WmNoRecordQuery queryParams) {
        Page<WmNoRecord> result = wmNoRecordService.getPage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "保存微盟交易单号表", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreventDuplicateSubmit
    @PreAuthorize("@ss.hasPerm('sys:wmNoRecord:save')")
    public Result<?> saveWmNoRecord(@RequestBody @Valid WmNoRecordForm wmNoRecordForm) {
        boolean result = wmNoRecordService.saveWmNoRecord(wmNoRecordForm);
        return Result.judge(result);
    }

    @Operation(summary = "获取微盟交易单号表信息", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{id}/form")
    public Result<WmNoRecordForm> getWmNoRecordFormData(@Parameter(description = "主键ID") @PathVariable Long id) {
        WmNoRecordForm wmNoRecord = wmNoRecordService.getWmNoRecordFormData(id);
        return Result.success(wmNoRecord);
    }

    @Operation(summary = "删除微盟交易单号表", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:wmNoRecord:delete')")
    public Result<?> deleteWmNoRecord(@Parameter(description = "ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = wmNoRecordService.deleteWmNoRecord(ids);
        return Result.judge(result);
    }

    @Operation(summary = "微盟交易单号表导入", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/wmNoRecordImport")
    @PreAuthorize("@ss.hasPerm('sys:wmNoRecord:import')")
    @PreventDuplicateSubmit
    public Result<?> wmNoRecordImport(@RequestParam("file") MultipartFile file) throws Exception {
        WmNoRecordListener listener = new WmNoRecordListener();
        EasyExcelFactory.read(file.getInputStream(), WmNoRecordImportVo.class, listener).sheet(0).doRead();
        List<WmNoRecordImportVo> list = listener.getVos();
        if (list == null || list.size() == 0) {
            return Result.success();
        }
        wmNoRecordService.importData(list);
        return Result.success();
    }

    @Operation(summary = "导出微盟交易单号表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/_export")
    @PreAuthorize("@ss.hasPerm('sys:wmNoRecord:export')")
    @PreventDuplicateSubmit
    public void export(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String fileName = "微盟交易单号表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));

        List<WmNoRecordImportVo> wmNoRecordList = wmNoRecordService.findByIds(ids);
        EasyExcel.write(response.getOutputStream(), WmNoRecordImportVo.class).sheet("微盟交易单号表").doWrite(wmNoRecordList);
    }
}

